﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EnvDTE;

namespace SPALM.ApplicationDeployment.Wizard
{
  public partial class WizardForm : Form
  {
    internal List<string> TargetProjects;
    internal List<string> AdditionalWSPs;

    public WizardForm(object automationObject)
    {
      InitializeComponent();

      LoadProjects(automationObject);

      comboBox_SharePointVersion.SelectedIndex = 1;
    }

    private void LoadProjects(object automationObject)
    {
      checkedListBox1.Items.Clear();
      if (automationObject is EnvDTE.DTE)
      {
        foreach(Project project in Helpers.GetAllProjects(automationObject as EnvDTE.DTE))
        {
          checkedListBox1.Items.Add(project.Name, true);
        }
      }
    }

    internal string get_CustomMessage()
    {
      return "hallo";
    }

    private void wizardControl1_FinishButtonClick(object sender, EventArgs e)
    {
      TargetProjects = new List<string>();
      if (checkBox_existingWPSs.Checked)
      {
        foreach (object s in checkedListBox1.CheckedItems)
        {
          TargetProjects.Add(s.ToString());
        }
      }

      AdditionalWSPs = new List<string>();
      if (checkBox_AdditionalWSPs.Checked)
      {
        if (this.textBox_wspfiles.Text != "")
        {
          char[] sep = new char[] { ';' };
          foreach (string s in this.textBox_wspfiles.Text.Split(sep))
          {
            AdditionalWSPs.Add(s.ToString());
          }
        }
      }

      this.DialogResult = System.Windows.Forms.DialogResult.OK;
      Close();
    }

    private void wizardControl1_CancelButtonClick(object sender, EventArgs e)
    {
      this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
      Close();
    }

    private void WizardForm_Load(object sender, EventArgs e)
    {

    }

    private void wizardControl1_HelpButtonClick(object sender, EventArgs e)
    {
      System.Diagnostics.Process process = new System.Diagnostics.Process();
      process.StartInfo.UseShellExecute = true;
      process.StartInfo.FileName = "http://spappdeploy.spalmblogger.de";
      process.Start();

    }

    private void checkBox_AdditionalWSPs_CheckedChanged(object sender, EventArgs e)
    {
      panel1.Enabled = checkBox_AdditionalWSPs.Checked;
    }

    private void checkBox_existingWPSs_CheckedChanged(object sender, EventArgs e)
    {
      panel2.Enabled = checkBox_existingWPSs.Checked;
    }

    private void openFileDialogWSP_FileOk(object sender, CancelEventArgs e)
    {
      this.textBox_wspfiles.Text = GetFilenames(openFileDialogWSP.FileNames);
    }

    private string GetFilenames(string[] p)
    {
      string result = "";
      foreach (string s in p)
      {
        if (result != "")
        {
          result += ";";
        }
        result += s;
      }
      return result;
    }

    private void button1_Click(object sender, EventArgs e)
    {
      openFileDialogWSP.ShowDialog();
    }

    private void label14_Click(object sender, EventArgs e)
    {

    }

    private void intermediateStep3_Click(object sender, EventArgs e)
    {

    }

    public string ForceSolutionDeployment { get { return checkBox_ForceSolutionDeployment.Checked.ToString().ToLower();  } }

    public string DeployToAllContentUrls { get { return checkBox_DeployToAllContentUrls.Checked.ToString().ToLower(); } }

    public string OverwriteExistingSolutions { get { return checkBox_OverwriteExistingSolutions.Checked.ToString().ToLower(); } }

    public string SharePointVersion { get { return comboBox_SharePointVersion.SelectedItem.ToString(); } }

    public string AllowGACDeployment { get { return checkBox_AllowGACDeployment.Checked.ToString().ToLower(); } }

    public string AllowCASPolicies { get { return checkBox_AllowCASPolicies.Checked.ToString().ToLower(); } }

    public string RestartSPTimer { get { return checkBox_RestartSPTimer.Checked.ToString().ToLower(); } }

    public string RestartSPAdmin { get { return checkBox_RestartSPAdmin.Checked.ToString().ToLower(); } }

    public string RestartSPUserCodeHost { get { return checkBox_RestartSPUserCodeHost.Checked.ToString().ToLower(); } }

    public string RestartIIS { get { return checkBox_RestartIIS.Checked.ToString().ToLower(); } }

    public string DisplayWizards { get { return checkBox_DisplayWizards.Checked.ToString().ToLower(); } }

    private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
      System.Diagnostics.Process process = new System.Diagnostics.Process();
      process.StartInfo.UseShellExecute = true;
      process.StartInfo.FileName = "http://spalm.codeplex.com";
      process.Start();
    }
  }
}
